package com.shanghai.shanghaielectric.DB;

import android.util.Log;
import android.widget.Toast;

import com.shanghai.shanghaielectric.Constants;
import com.shanghai.shanghaielectric.DataAdapter.Cells2ShowAdapter;
import com.shanghai.shanghaielectric.DataAdapter.ReportDataHelper;
import com.shanghai.shanghaielectric.Model.db.ErrorInfoModel;
import com.shanghai.shanghaielectric.Model.ReceiveDataModel;
import com.shanghai.shanghaielectric.Model.db.OperateInfoModel;
import com.shanghai.shanghaielectric.Model.db.ReceiveDataModel_10s;
import com.shanghai.shanghaielectric.Model.ReportDateModel;
import com.shanghai.shanghaielectric.Model.db.ReportDateModel_1day;
import com.shanghai.shanghaielectric.Model.WarningInfoModel;
import com.shanghai.shanghaielectric.MyApplication;

import org.greenrobot.eventbus.EventBus;
import org.litepal.crud.DataSupport;
import org.litepal.crud.callback.FindCallback;
import org.litepal.crud.callback.FindMultiCallback;
import org.litepal.crud.callback.SaveCallback;
import org.litepal.crud.callback.UpdateOrDeleteCallback;


import java.util.Date;
import java.util.List;

import shanghaielectric_lib.DB.DBHelper_config;
import shanghaielectric_lib.DeviceConfigModel.Point.PointEnumData;
import shanghaielectric_lib.MyEventBus.EventBus_showCells;
import shanghaielectric_lib.Utils.DateUtil;
import shanghaielectric_lib.Utils.LogUtil;
import shanghaielectric_lib.Utils.dwin.BitUtils;


/**
 * Created by 45000440 on 2017/8/30.
 */

public class LitePalHelper {//Android Postfix Completion代码自动补全

    public static void insertErrorInfo(EventBus_showCells eventBusShowCells) {
        final boolean[] isErrorInfoChanged = {false};
        final boolean[] isWarnningInfoChanged = {false};

        int deviceID = eventBusShowCells.deviceID;
        ReceiveDataModel receiveDataModel = new Cells2ShowAdapter().cells2ReceiveDataModel(eventBusShowCells.cell_showMap);
        int errorCode1 = Integer.valueOf(receiveDataModel.errorCode_1_real);
        int errorCode2 = Integer.valueOf(receiveDataModel.errorCode_2_real);
        int errorCode3 = Integer.valueOf(receiveDataModel.errorCode_3_real);
        int errorCode4 = Integer.valueOf(receiveDataModel.errorCode_4_real);
        int errorCode5 = Integer.valueOf(receiveDataModel.errorCode_5_real);

        int alertCode1 = Integer.valueOf(receiveDataModel.alertCode_1_real);
        int alertCode2 = Integer.valueOf(receiveDataModel.alertCode_2_real);
        int alertCode3 = Integer.valueOf(receiveDataModel.alertCode_3_real);
        int alertCode4 = Integer.valueOf(receiveDataModel.alertCode_4_real);

        if (errorCode1 != Constants.errorCode1) {
            for (int i = 0; i < 16; i++) {
                if (BitUtils.getBit(errorCode1, i) != BitUtils.getBit(Constants.errorCode1, i)) {
                    if (BitUtils.getBit(errorCode1, i)) {
                        //出现新故障
                        ErrorInfoModel errorInfo = new ErrorInfoModel();
                        errorInfo.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        errorInfo.setErrorDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(84);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                errorInfo.setErrorContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal" + "新增加error" + pointEnumDataList.get(j).enumValue);

                            }
                        }
                        errorInfo.setErrorType(1);
                        errorInfo.setErrorBit(i);
                        errorInfo.setClear(0);
                        errorInfo.saveAsync().listen(new SaveCallback() {
                            @Override
                            public void onFinish(boolean success) {
                                isErrorInfoChanged[0] = true;
//                                LogUtil.e("litepal", "onFinish--增加");
                                EventBus.getDefault().post(new ErrorInfoModel());
                            }
                        });
                        Constants.errorinfo_last_1 = errorInfo;
                    } else {
                        //故障消除
                        ErrorInfoModel errorInfo = new ErrorInfoModel();
                        errorInfo.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        errorInfo.setErrorDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(84);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                errorInfo.setErrorContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal" + "消除error" + pointEnumDataList.get(j).enumValue);
                            }
                        }
                        errorInfo.setErrorType(1);
                        errorInfo.setErrorBit(i);
                        errorInfo.setClear(1);
                        errorInfo.updateAllAsync("errordate = ?", Constants.errorinfo_last_1.getErrorDate()).listen(new UpdateOrDeleteCallback() {
                            @Override
                            public void onFinish(int rowsAffected) {
                                LogUtil.e("litepal" + "onFinish---消除");
                                isErrorInfoChanged[0] = true;
                                EventBus.getDefault().post(new ErrorInfoModel());

                            }
                        });

                    }
                }
            }
            Constants.errorCode1 = errorCode1;
        }
        //errorCode2
        if (errorCode2 != Constants.errorCode2) {
            for (int i = 0; i < 16; i++) {
                if (BitUtils.getBit(errorCode2, i) != BitUtils.getBit(Constants.errorCode2, i)) {
                    if (BitUtils.getBit(errorCode2, i)) {//说明 0 ---> 1
                        //出现新故障
                        ErrorInfoModel errorInfo = new ErrorInfoModel();
                        errorInfo.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        errorInfo.setErrorDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(85);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                errorInfo.setErrorContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal" + "新增加error" + pointEnumDataList.get(j).enumValue + "---" + errorCode2 + "---" + Constants.errorCode2);
                            }
                        }
                        errorInfo.setErrorType(2);
                        errorInfo.setErrorBit(i);
                        errorInfo.setClear(0);
                        errorInfo.saveAsync().listen(new SaveCallback() {
                            @Override
                            public void onFinish(boolean success) {
                                LogUtil.e("litepal" + "onFinish--增加");
                                isErrorInfoChanged[0] = true;
                                EventBus.getDefault().post(new ErrorInfoModel());
                            }
                        });
                        Constants.errorinfo_last_2 = errorInfo;
                    } else {
                        //故障消除
                        ErrorInfoModel errorInfo = new ErrorInfoModel();
                        errorInfo.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        errorInfo.setErrorDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(85);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                errorInfo.setErrorContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal" + "消除error" + pointEnumDataList.get(j).enumValue);
                            }
                        }
                        errorInfo.setErrorType(2);
                        errorInfo.setErrorBit(i);
                        errorInfo.setClear(1);
                        errorInfo.updateAllAsync("errordate = ?", Constants.errorinfo_last_2.getErrorDate()).listen(new UpdateOrDeleteCallback() {
                            @Override
                            public void onFinish(int rowsAffected) {
                                LogUtil.e("litepal" + "onFinish---消除");
                                isErrorInfoChanged[0] = true;
                                EventBus.getDefault().post(new ErrorInfoModel());

                            }
                        });
                    }
                }
            }
            Constants.errorCode2 = errorCode2;
        }
        //errorCode3
        if (errorCode3 != Constants.errorCode3) {
            for (int i = 0; i < 16; i++) {
                if (BitUtils.getBit(errorCode3, i) != BitUtils.getBit(Constants.errorCode3, i)) {
                    if (BitUtils.getBit(errorCode3, i)) {
                        //出现新故障
                        ErrorInfoModel errorInfo = new ErrorInfoModel();
                        errorInfo.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        errorInfo.setErrorDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(86);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                errorInfo.setErrorContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal" + "新增加error" + pointEnumDataList.get(j).enumValue);
                            }
                        }
                        errorInfo.setErrorType(3);
                        errorInfo.setErrorBit(i);
                        errorInfo.setClear(0);
                        errorInfo.saveAsync().listen(new SaveCallback() {
                            @Override
                            public void onFinish(boolean success) {

                                LogUtil.e("litepal" + "onFinish--增加");
                                isErrorInfoChanged[0] = true;
                                EventBus.getDefault().post(new ErrorInfoModel());

                            }
                        });

                        Constants.errorinfo_last_3 = errorInfo;

                    } else {
                        //故障消除
                        ErrorInfoModel errorInfo = new ErrorInfoModel();
                        errorInfo.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        errorInfo.setErrorDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(86);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                errorInfo.setErrorContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal", "消除error" + pointEnumDataList.get(j).enumValue);
                            }
                        }
                        errorInfo.setErrorType(3);
                        errorInfo.setErrorBit(i);
                        errorInfo.setClear(1);
                        errorInfo.updateAllAsync("errordate = ?", Constants.errorinfo_last_3.getErrorDate()).listen(new UpdateOrDeleteCallback() {
                            @Override
                            public void onFinish(int rowsAffected) {
                                LogUtil.e("litepal" + "onFinish---消除");
                                EventBus.getDefault().post(new ErrorInfoModel());

                                isErrorInfoChanged[0] = true;
                            }
                        });
                    }
                }
            }
            Constants.errorCode3 = errorCode3;
        }

        //errorCode4
        if (errorCode4 != Constants.errorCode4) {
            for (int i = 0; i < 16; i++) {
                if (BitUtils.getBit(errorCode4, i) != BitUtils.getBit(Constants.errorCode4, i)) {
                    if (BitUtils.getBit(errorCode4, i)) {
                        //出现新故障
                        ErrorInfoModel errorInfo = new ErrorInfoModel();
                        errorInfo.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        errorInfo.setErrorDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(87);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                errorInfo.setErrorContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal", "新增加error" + pointEnumDataList.get(j).enumValue);
                            }
                        }
                        errorInfo.setErrorType(4);
                        errorInfo.setErrorBit(i);
                        errorInfo.setClear(0);
                        errorInfo.saveAsync().listen(new SaveCallback() {
                            @Override
                            public void onFinish(boolean success) {
                                isErrorInfoChanged[0] = true;
                                LogUtil.e("litepal", "onFinish--增加");
                                EventBus.getDefault().post(new ErrorInfoModel());

                            }
                        });
                        Constants.errorinfo_last_4 = errorInfo;

                    } else {
                        //故障消除
                        ErrorInfoModel errorInfo = new ErrorInfoModel();
                        errorInfo.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        errorInfo.setErrorDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(87);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                errorInfo.setErrorContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal", "消除error" + pointEnumDataList.get(j).enumValue);
                            }
                        }
                        errorInfo.setErrorType(4);
                        errorInfo.setErrorBit(i);
                        errorInfo.setClear(1);
                        errorInfo.updateAllAsync("errordate = ?", Constants.errorinfo_last_4.getErrorDate()).listen(new UpdateOrDeleteCallback() {
                            @Override
                            public void onFinish(int rowsAffected) {
                                LogUtil.e("litepal", "onFinish---消除");
                                EventBus.getDefault().post(new ErrorInfoModel());

                                isErrorInfoChanged[0] = true;
                            }
                        });
                    }
                }
            }
            Constants.errorCode4 = errorCode4;
        }

        //errorCode5
        if (errorCode5 != Constants.errorCode5) {
            for (int i = 0; i < 16; i++) {
                if (BitUtils.getBit(errorCode5, i) != BitUtils.getBit(Constants.errorCode5, i)) {
                    if (BitUtils.getBit(errorCode5, i)) {
                        //出现新故障
                        ErrorInfoModel errorInfo = new ErrorInfoModel();
                        errorInfo.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        errorInfo.setErrorDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(88);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                errorInfo.setErrorContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal", "新增加error" + pointEnumDataList.get(j).enumValue);
                            }
                        }
                        errorInfo.setErrorType(5);
                        errorInfo.setErrorBit(i);
                        errorInfo.setClear(0);
                        errorInfo.saveAsync().listen(new SaveCallback() {
                            @Override
                            public void onFinish(boolean success) {
                                EventBus.getDefault().post(new ErrorInfoModel());

                                LogUtil.e("litepal", "onFinish--增加");
                                isErrorInfoChanged[0] = true;
                            }
                        });
                        Constants.errorinfo_last_5 = errorInfo;
                    } else {
                        //故障消除
                        ErrorInfoModel errorInfo = new ErrorInfoModel();
                        errorInfo.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        errorInfo.setErrorDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(88);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                errorInfo.setErrorContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal", "消除error" + pointEnumDataList.get(j).enumValue);

                            }
                        }
                        errorInfo.setErrorType(5);
                        errorInfo.setErrorBit(i);
                        errorInfo.setClear(1);
                        errorInfo.updateAllAsync("errordate = ?", Constants.errorinfo_last_5.getErrorDate()).listen(new UpdateOrDeleteCallback() {
                            @Override
                            public void onFinish(int rowsAffected) {
                                LogUtil.e("litepal", "onFinish---消除");
                                isErrorInfoChanged[0] = true;
                                EventBus.getDefault().post(new ErrorInfoModel());


                            }
                        });

                    }
                }
            }
            Constants.errorCode5 = errorCode5;
        }


        //alertCode1
        if (alertCode1 != Constants.warningCode1) {
            for (int i = 0; i < 16; i++) {
                if (BitUtils.getBit(alertCode1, i) != BitUtils.getBit(Constants.warningCode1, i)) {
                    if (BitUtils.getBit(alertCode1, i)) {
                        //出现新故障
                        WarningInfoModel warningInfoModel = new WarningInfoModel();
                        warningInfoModel.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        warningInfoModel.setAlertDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(68);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                warningInfoModel.setAlertContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal", "新增加error" + pointEnumDataList.get(j).enumValue);
                            }
                        }
                        warningInfoModel.setErrorType(1);
                        warningInfoModel.setErrorBit(i);
                        warningInfoModel.setIsClear(0);
                        warningInfoModel.saveAsync().listen(new SaveCallback() {
                            @Override
                            public void onFinish(boolean success) {
                                isWarnningInfoChanged[0] = true;
                                LogUtil.e("litepal", "onFinish--增加");
                                EventBus.getDefault().post(new WarningInfoModel());

                            }
                        });
                        Constants.warningInfo_last_l = warningInfoModel;

                    } else {
                        //故障消除
                        WarningInfoModel warningInfoModel = new WarningInfoModel();
                        warningInfoModel.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        warningInfoModel.setAlertDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(68);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                warningInfoModel.setAlertContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal", "消除error" + pointEnumDataList.get(j).enumValue);
                            }
                        }
                        warningInfoModel.setErrorType(1);
                        warningInfoModel.setErrorBit(i);
                        warningInfoModel.setIsClear(1);
                        warningInfoModel.updateAllAsync("alertdate = ?", Constants.warningInfo_last_l.getAlertDate()).listen(new UpdateOrDeleteCallback() {
                            @Override
                            public void onFinish(int rowsAffected) {
                                LogUtil.e("litepal", "onFinish---消除");
                                EventBus.getDefault().post(new WarningInfoModel());

                                isWarnningInfoChanged[0] = true;
                            }
                        });

                    }
                }
            }
            Constants.warningCode1 = alertCode1;
        }
        //alertCode2
        if (alertCode2 != Constants.warningCode2) {
            for (int i = 0; i < 16; i++) {
                if (BitUtils.getBit(alertCode2, i) != BitUtils.getBit(Constants.warningCode2, i)) {
                    if (BitUtils.getBit(alertCode2, i)) {
                        //出现新故障
                        WarningInfoModel warningInfoModel = new WarningInfoModel();
                        warningInfoModel.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        warningInfoModel.setAlertDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(69);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                warningInfoModel.setAlertContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal", "新增加error" + pointEnumDataList.get(j).enumValue);
                            }
                        }
                        warningInfoModel.setErrorType(2);
                        warningInfoModel.setErrorBit(i);
                        warningInfoModel.setIsClear(0);
                        warningInfoModel.saveAsync().listen(new SaveCallback() {
                            @Override
                            public void onFinish(boolean success) {
                                isWarnningInfoChanged[0] = true;
                                LogUtil.e("litepal", "onFinish--增加");
                                EventBus.getDefault().post(new WarningInfoModel());

                            }
                        });
                        Constants.warningInfo_last_2 = warningInfoModel;

                    } else {
                        //故障消除
                        WarningInfoModel warningInfoModel = new WarningInfoModel();
                        warningInfoModel.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        warningInfoModel.setAlertDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(69);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                warningInfoModel.setAlertContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal", "消除error" + pointEnumDataList.get(j).enumValue);
                            }
                        }
                        warningInfoModel.setErrorType(2);
                        warningInfoModel.setErrorBit(i);
                        warningInfoModel.setIsClear(1);
                        warningInfoModel.updateAllAsync("alertdate = ?", Constants.warningInfo_last_2.getAlertDate()).listen(new UpdateOrDeleteCallback() {
                            @Override
                            public void onFinish(int rowsAffected) {
                                LogUtil.e("litepal", "onFinish---消除");
                                isWarnningInfoChanged[0] = true;
                                EventBus.getDefault().post(new WarningInfoModel());


                            }
                        });

                    }
                }
            }
            Constants.warningCode2 = alertCode2;
        }
        //alertCode3
        if (alertCode3 != Constants.warningCode3) {
            for (int i = 0; i < 16; i++) {
                if (BitUtils.getBit(alertCode3, i) != BitUtils.getBit(Constants.warningCode3, i)) {
                    if (BitUtils.getBit(alertCode3, i)) {
                        //出现新故障
                        WarningInfoModel warningInfoModel = new WarningInfoModel();
                        warningInfoModel.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        warningInfoModel.setAlertDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(70);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                warningInfoModel.setAlertContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal", "新增加error" + pointEnumDataList.get(j).enumValue);
                            }
                        }
                        warningInfoModel.setErrorType(3);
                        warningInfoModel.setErrorBit(i);
                        warningInfoModel.setIsClear(0);
                        warningInfoModel.saveAsync().listen(new SaveCallback() {
                            @Override
                            public void onFinish(boolean success) {
                                isWarnningInfoChanged[0] = true;
                                LogUtil.e("litepal", "onFinish--增加");
                                EventBus.getDefault().post(new WarningInfoModel());

                            }
                        });
                        Constants.warningInfo_last_3 = warningInfoModel;

                    } else {
                        //故障消除
                        WarningInfoModel warningInfoModel = new WarningInfoModel();
                        warningInfoModel.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        warningInfoModel.setAlertDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(70);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                warningInfoModel.setAlertContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal", "消除error" + pointEnumDataList.get(j).enumValue);
                            }
                        }
                        warningInfoModel.setErrorType(3);
                        warningInfoModel.setErrorBit(i);
                        warningInfoModel.setIsClear(1);
                        warningInfoModel.updateAllAsync("alertdate = ?", Constants.warningInfo_last_3.getAlertDate()).listen(new UpdateOrDeleteCallback() {
                            @Override
                            public void onFinish(int rowsAffected) {
                                LogUtil.e("litepal", "onFinish---消除");
                                isWarnningInfoChanged[0] = true;
                                EventBus.getDefault().post(new WarningInfoModel());


                            }
                        });

                    }
                }
            }
            Constants.warningCode3 = alertCode3;
        }


        //alertCode4
        if (alertCode4 != Constants.warningCode4) {
            for (int i = 0; i < 16; i++) {
                if (BitUtils.getBit(alertCode4, i) != BitUtils.getBit(Constants.warningCode4, i)) {
                    if (BitUtils.getBit(alertCode4, i)) {
                        //出现新故障
                        WarningInfoModel warningInfoModel = new WarningInfoModel();
                        warningInfoModel.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        warningInfoModel.setAlertDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(71);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                warningInfoModel.setAlertContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal", "新增加error" + pointEnumDataList.get(j).enumValue);
                            }
                        }
                        warningInfoModel.setErrorType(4);
                        warningInfoModel.setErrorBit(i);
                        warningInfoModel.setIsClear(0);
                        warningInfoModel.saveAsync().listen(new SaveCallback() {
                            @Override
                            public void onFinish(boolean success) {
                                isWarnningInfoChanged[0] = true;
                                LogUtil.e("litepal", "onFinish--增加");
                                EventBus.getDefault().post(new WarningInfoModel());

                            }
                        });
                        Constants.warningInfo_last_4 = warningInfoModel;

                    } else {
                        //故障消除
                        WarningInfoModel warningInfoModel = new WarningInfoModel();
                        warningInfoModel.setUnit_ID(deviceID);
                        Date date = new Date(System.currentTimeMillis());
                        warningInfoModel.setAlertDate(date.getTime() + "");
                        List<PointEnumData> pointEnumDataList = DBHelper_config.getDBHelper_config().getPointEnumDataListbyPointID_(71);
                        for (int j = 0; j < pointEnumDataList.size(); j++) {
                            if (pointEnumDataList.get(j).enumKey == i) {
                                warningInfoModel.setAlertContent(pointEnumDataList.get(j).enumValue);
                                LogUtil.e("litepal", "消除error" + pointEnumDataList.get(j).enumValue);
                            }
                        }
                        warningInfoModel.setErrorType(4);
                        warningInfoModel.setErrorBit(i);
                        warningInfoModel.setIsClear(1);
                        warningInfoModel.updateAllAsync("alertdate = ?", Constants.warningInfo_last_4.getAlertDate()).listen(new UpdateOrDeleteCallback() {
                            @Override
                            public void onFinish(int rowsAffected) {
                                LogUtil.e("litepal", "onFinish---消除");
                                isWarnningInfoChanged[0] = true;
                                EventBus.getDefault().post(new WarningInfoModel());


                            }
                        });

                    }
                }
            }
            Constants.warningCode4 = alertCode4;
        }
//
//        if (isErrorInfoChanged[0]){
//            EventBus.getDefault().post(new ErrorInfoModel());
//            LogUtil.e("error","发布");
//        }
//        if (isWarnningInfoChanged[0]){
//            EventBus.getDefault().post(new WarningInfoModel());
//        }
    }


    public static void insertOperateInfo(String info) {
        OperateInfoModel operateInfoModel = new OperateInfoModel();
        operateInfoModel.setOperateContent(info);
        operateInfoModel.setOperateDate(System.currentTimeMillis());
        operateInfoModel.saveAsync().listen(new SaveCallback() {
            @Override
            public void onFinish(boolean success) {
                EventBus.getDefault().post(new OperateInfoModel());
            }
        });
    }

    public static void deleteDBInfoTo_100() {
        DataSupport.findLastAsync(ErrorInfoModel.class).listen(new FindCallback() {
            @Override
            public <T> void onFinish(T t) {
                ErrorInfoModel errorInfoModel = (ErrorInfoModel) t;
                long id = errorInfoModel.getId();

//                Toast.makeText(MyApplication.getContext(), "最后一条数据的id" + id, Toast.LENGTH_SHORT).show();
                if (id > 99) {
                    DataSupport.deleteAllAsync(ErrorInfoModel.class, "id < ?", (id - 99) + "").listen(new UpdateOrDeleteCallback() {
                        @Override
                        public void onFinish(int rowsAffected) {
                            Toast.makeText(MyApplication.getContext(), "错误信息清理完毕,剩余100条记录", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });

        DataSupport.findLastAsync(OperateInfoModel.class).listen(new FindCallback() {
            @Override
            public <T> void onFinish(T t) {
                OperateInfoModel operateInfoModel = (OperateInfoModel) t;
                long id = operateInfoModel.getId();

//                Toast.makeText(MyApplication.getContext(), "最后一条数据的id" + id, Toast.LENGTH_SHORT).show();
                if (id > 99) {
                    DataSupport.deleteAllAsync(OperateInfoModel.class, "id < ?", (id - 99) + "").listen(new UpdateOrDeleteCallback() {
                        @Override
                        public void onFinish(int rowsAffected) {
                            Toast.makeText(MyApplication.getContext(), "操作记录清理完毕,剩余100条记录", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }


    /**
     * 插入报表数据
     */
    public static void insertReportData(final ReceiveDataModel receiveDataModel) {


        DataSupport.findLastAsync(ReportDateModel.class).listen(new FindCallback() {
            @Override
            public <T> void onFinish(T t) {
                if (t != null) {
                    ReportDataHelper reportDataHelper = new ReportDataHelper();
                    ReportDateModel reportDateModel = reportDataHelper.homeDataModelToReportData(receiveDataModel, (ReportDateModel) t);
                    reportDateModel.saveAsync().listen(new SaveCallback() {
                        @Override
                        public void onFinish(boolean success) {

                        }
                    });
                } else {
                    ReportDataHelper reportDataHelper = new ReportDataHelper();
                    ReportDateModel reportDateModel = reportDataHelper.homeDataModelToReportData(receiveDataModel, new ReportDateModel());
                    reportDateModel.saveAsync().listen(new SaveCallback() {
                        @Override
                        public void onFinish(boolean success) {

                        }
                    });
                }
            }
        });

    }

    /**
     * 插入10s数据
     */
    public static void insertReceiveDataModel_10s(ReceiveDataModel_10s receiveDataModel_10s) {
        receiveDataModel_10s.saveAsync().listen(new SaveCallback() {
            @Override
            public void onFinish(boolean success) {

            }
        });
    }


    /**
     *  从 10s 表 取上一个小时数据，计算 然后结果 存入1H 表里
     */

    public static void insertReportDateTo_1h() {
        //计算上一个小时完整时间段
        long endTime= DateUtil.getCurrentSystemTime_yyyy_MM_dd_HH_long();
        long startTime=endTime-3600000;
        DataSupport.where("date > ? and date < ?" , startTime+"",endTime+"").findAsync(ReceiveDataModel_10s.class).listen(new FindMultiCallback() {
            @Override
            public <T> void onFinish(List<T> t) {
                //查询到结果，计算后存入小时 表里
                if (t != null && t.size()!=0) {
                    List<ReceiveDataModel_10s> receiveDataModel_10s = (List<ReceiveDataModel_10s>) t;
                    Log.e("cacaca","开始计算");

                    ReportDateModel reportDateModel = ReportDataHelper.calculate_10s_ReceiveDataModel(receiveDataModel_10s);
                    Log.e("cacaca","计算结束");
//得到集合数据，进行计算
                    reportDateModel.saveAsync().listen(new SaveCallback() {
                        @Override
                        public void onFinish(boolean success) {
                            Log.e("cacaca","计算结束");

                        }
                    });

                }

            }
        });
    }


    /**
     *  从 1h 表 取上一天数据，计算 然后结果 存入1day 表里
     */

    public static void insertReportDateTo_1day() {
        long endTime= DateUtil.getSystemTime_yyyy_MM_dd_long();
        long startTime=endTime-24*3600000;
        DataSupport.where("date > ? and date < ?" , startTime+"",endTime+"").findAsync(ReportDateModel.class).listen(new FindMultiCallback() {
            @Override
            public <T> void onFinish(List<T> t) {
                //查询到结果，计算后存入小时 表里
                if (t != null&&t.size()!=0) {
                    List<ReportDateModel> reportDateModel1hs = (List<ReportDateModel>) t;
                    ReportDateModel_1day reportDateModel1day = ReportDataHelper.calculate_1h_ReceiveDataModel_1day(reportDateModel1hs);
//得到集合数据，进行计算
                    reportDateModel1day.saveAsync().listen(new SaveCallback() {
                        @Override
                        public void onFinish(boolean success) {

                            //一天成功结算后，删除10s表所有数据



                        }
                    });

                }

            }
        });
    }




}
